package com.nanako.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.nanako.dao.SetmealMapper;
import com.nanako.dto.SetmealDto;
import com.nanako.entity.Category;
import com.nanako.entity.Setmeal;
import com.nanako.entity.SetmealDish;
import com.nanako.exception.CascadingDeleteException;
import com.nanako.service.CategoryService;
import com.nanako.service.DishService;
import com.nanako.service.SetmealDishService;
import com.nanako.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional

public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Lazy
    @Autowired
    private CategoryService categoryService;


    public boolean addSetmeal(SetmealDto setmealDto) {
        if (this.save(setmealDto)) {
            setmealDto.getSetmealDishes()
                    .forEach(setmealDish -> {
                        setmealDish.setSetmealId(setmealDto.getId());
                        setmealDishService.save(setmealDish);
                    });
            return true;
        }
        return false;
    }

    @Override
    public void removeSetmealByIds(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = this.getById(id);
            if (setmeal.getStatus() == 1) {
                throw new CascadingDeleteException("当前套餐处于售卖状态！！");
            }
            this.removeById(id);
            setmealDishService.remove(new LambdaUpdateWrapper<SetmealDish>()
                    .eq(SetmealDish::getSetmealId, id));
        });
    }

    @Override
    public SetmealDto queryOne(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
        Category category = categoryService.getById(setmeal.getCategoryId());
        setmealDto.setSetmealDishes(setmealDishes);
        setmealDto.setCategoryName(category.getName());
        BeanUtils.copyProperties(setmeal, setmealDto);
        return setmealDto;
    }

    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        setmealDto.getSetmealDishes().forEach(setmealDish -> {
            setmealDishService.remove(new LambdaUpdateWrapper<SetmealDish>()
                    .eq(SetmealDish::getSetmealId, setmealDto.getId()));
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishService.save(setmealDish);
        });
    }
}
